/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package elaborate.tool.messenger.yahoo;

import java.io.InputStream;
import java.util.List;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;

/**
 *
 * @author lendle
 */
public class HttpUtil {

    private String oauth_consumer_key;
    private String oauth_token;
    private String consumer_secret;
    private String oauthSecret;
    private String sessionId;
    private String userId = null;
    private static double oauth_nonce = 24829.2330;

    public HttpUtil(String oauth_consumer_key, String consumer_secret, String oauth_token, String oauthSecret, String sessionId, String userId) {
        this.oauth_consumer_key = oauth_consumer_key;
        this.oauth_token = oauth_token;
        this.consumer_secret = consumer_secret;
        this.oauthSecret = oauthSecret;
        this.sessionId = sessionId;
        this.userId = userId;
    }

    private String getOauthCredential() {
        long timestamp = ((long) (System.currentTimeMillis() / 1000));
        String oauthCredential = "OAuth realm=\"yahooapis.com\","
                + "oauth_consumer_key=\"" + this.oauth_consumer_key + "\","
                + "oauth_nonce=\"" + (oauth_nonce++) + "\","
                + "oauth_signature_method=\"PLAINTEXT\","
                + "oauth_timestamp=\"" + timestamp + "\","
                + "oauth_token=\"" + this.oauth_token + "\","
                + "oauth_version=\"1.0\","
                + "oauth_signature=\"" + this.consumer_secret + "%26" + this.oauthSecret + "\"";
        return oauthCredential;
    }

    private String readLines(InputStream input) throws Exception {
        List<String> list = IOUtils.readLines(input, "utf-8");
        StringBuffer buffer = new StringBuffer();
        for (String str : list) {
            buffer.append(str).append("\r\n");
        }
        return buffer.toString();
    }

    public String doGet(String url) throws Exception {
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpGet httpget = new HttpGet(url);

        httpget.setHeader("Content-Type", "application/json;charset=utf-8");
        httpget.setHeader("Authorization", this.getOauthCredential());

        HttpResponse response = httpClient.execute(httpget);
        HttpEntity entity = response.getEntity();
        return this.readLines(entity.getContent());
    }

    public HttpResponse doPost(String url, String content) throws Exception {
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpost = new HttpPost(url);

        httpost.setHeader("Content-Type", "application/json;charset=utf-8");
        httpost.setHeader("Authorization", this.getOauthCredential());

        StringEntity entity = new StringEntity(content, "utf-8");
        httpost.setEntity(entity);
        return httpClient.execute(httpost);
    }
    
    public HttpResponse doDelete(String url) throws Exception{
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpDelete httpDelete = new HttpDelete(url);
        httpDelete.setHeader("Content-Type", "application/json;charset=utf-8");
        String oauthCredential = getOauthCredential();
        httpDelete.setHeader("Authorization", oauthCredential);
        return httpClient.execute(httpDelete);
    }
}
